<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
      label-width="100px">
      <el-form-item v-if="!dataForm.id" label="类型" prop="type">
        <el-radio-group v-model="dataForm.type">
          <el-radio v-for="(type, index) in dataForm.typeList" :label="index" :key="index">{{ type }}</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item v-if="dataForm.type === 0" label="配置项名称" prop="displayName">
        <el-input v-model="dataForm.displayName" maxlength="50" show-word-limit placeholder="请填写配置项名称"></el-input>
      </el-form-item>

      <el-form-item v-if="!dataForm.id && dataForm.type === 0" label="所属配置" prop="parentId">
        <el-select v-model="dataForm.parentId" placeholder="请选择">
          <el-option label="申请账号类型" value="1"></el-option>
          <el-option label="申请物资种类" value="3"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item v-if="dataForm.type === 1" label="模板名称" prop="displayName">
        <el-input v-model="dataForm.displayName" maxlength="50" :disabled="true" show-word-limit
          placeholder="请选择模板附件"></el-input>
      </el-form-item>

      <el-form-item v-if="!dataForm.id && dataForm.type === 1" label="模板类型" prop="templateType">
        <el-select v-model="dataForm.templateType" placeholder="请选择">
          <el-option label="离职申请模板" value="0"></el-option>
          <el-option label="请假申请模板" value="1"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item v-if="dataForm.type === 1" label="附件" prop="fileUrl">
        <single-upload v-model="dataForm.fileUrl" @change="costPlannedAmountChange"></single-upload>
      </el-form-item>

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import SingleUpload from '@/components/upload/singleUpload'
  export default {
    data() {
      return {
        visible: false,
        dataForm: {
          id: 0,
          parentId: undefined,
          type: 0,
          typeList: ['配置项', '模板'],
          fileUrl: undefined,
          templateType: undefined,
          displayName: undefined
        },
        dataRule: {
          name: [
            { required: true, message: '所属申请类型不能为空', trigger: 'blur' }
          ],
          displayName: [
            { required: true, message: '配置项或模板名称不能为空', trigger: 'blur' }
          ],
          parentId: [
            { required: true, message: '所属配置不能为空', trigger: 'blur' }
          ],
          templateType: [
            { required: true, message: '模板类型不能为空', trigger: 'blur' }
          ],
          fileUrl: [
            { required: true, message: '附件不能为空', trigger: 'blur' }
          ]
        },
        configList: [],
        configListTreeProps: {
          label: 'displayName',
          children: 'children'
        }
      }
    },
    components: {
      SingleUpload
    },
    methods: {
      init(id) {
        this.dataForm.id = id || undefined
        this.visible = true
        this.dataForm.parentId = undefined
        this.dataForm.type = 0
        this.dataForm.fileUrl = undefined
        this.dataForm.templateType = undefined
        this.dataForm.displayName = undefined
        // 修改
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/sys/config/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({ data }) => {
            this.dataForm.id = data.sysConfig.id
            this.dataForm.type = data.sysConfig.type
            this.dataForm.displayName = data.sysConfig.displayName
            this.dataForm.templateType = data.sysConfig.templateType
            this.dataForm.parentId = data.sysConfig.parentId
          })
        }
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sys/config/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'displayName': this.dataForm.displayName,
                'parentId': this.dataForm.parentId,
                'type': this.dataForm.type,
                'fileUrl': this.dataForm.fileUrl,
                'templateType': this.dataForm.templateType
              })
            }).then(({ data }) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      async costPlannedAmountChange(url, name) {
        this.dataForm.fileUrl = url
        this.dataForm.displayName = name
      }
    }
  }
</script>

<style lang="scss">

</style>